
// <copyright file="WizardUserControl.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   Class WizardUserControl.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Windows.Forms
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    ///     Class WizardUserControl.
    /// </summary>
    public partial class WizardUserControl : UserControl
    {
        #region Constructors and Finalizers

        /// <summary>
        ///     Initializes a new instance of the <see cref="WizardUserControl" /> class.
        /// </summary>
        public WizardUserControl()
        {
            this.InitializeComponent();
            this.backButton.Enabled = false;
        }

        #endregion Constructors and Finalizers

        #region Public Events

        /// <summary>
        ///     Occurs when [configuration wizard event].
        /// </summary>
        public event EventHandler<WizardEventArgs> OnWizardEvent;

        #endregion Public Events

        #region Public Properties

        /// <summary>
        ///     Gets or sets a value indicating whether [hide title].
        /// </summary>
        /// <value><c>true</c> if [hide title]; otherwise, <c>false</c>.</value>
        public bool HideTitle
        {
            get
            {
                return this.titleLabel.Visible;
            }

            set
            {
                this.titleLabel.Visible = value;
            }
        }

        /// <summary>
        ///     Gets the tab control.
        /// </summary>
        /// <value>The tab control.</value>
        public WizardTabControl TabControl
        {
            get
            {
                return this.wizardTabControl1;
            }
        }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        ///     Initializes the state.
        /// </summary>
        public void InitializeState()
        {
            if (this.wizardTabControl1.TabCount < 1)
            {
                this.nextButton.Text = @"&Finish";
            }
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// Handles the Load event of the WizardUserControl control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="EventArgs"/> instance containing the event data.
        /// </param>
        private void WizardUserControl_Load(object sender, EventArgs e)
        {
            // InitializeState();
        }

        /// <summary>
        /// Handles the Click event of the backButton control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="EventArgs"/> instance containing the event data.
        /// </param>
        private void backButton_Click(object sender, EventArgs e)
        {
            this.performEvent(OperationType.Back);
        }

        /// <summary>
        /// Handles the Click event of the cancelButton control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="EventArgs"/> instance containing the event data.
        /// </param>
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.performEvent(OperationType.Cancel);
        }

        /// <summary>
        ///     Gets the index of the selected tab.
        /// </summary>
        /// <returns>System.Int32.</returns>
        private int getSelectedTabIndex()
        {
            return this.wizardTabControl1.TabPages.IndexOf(this.wizardTabControl1.SelectedTab);
        }

        /// <summary>
        /// Handles the Click event of the nextButton control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="EventArgs"/> instance containing the event data.
        /// </param>
        private void nextButton_Click(object sender, EventArgs e)
        {
            this.performEvent(OperationType.Next);
        }

        /// <summary>
        /// Performs the event.
        /// </summary>
        /// <param name="operationType">
        /// Type of the operation.
        /// </param>
        private void performEvent(OperationType operationType)
        {
            int selectedTabIndex = this.wizardTabControl1.SelectedIndex;
            var args = new WizardEventArgs(operationType, selectedTabIndex);
            if (this.OnWizardEvent != null)
            {
                this.OnWizardEvent(this, args);
            }

            if (!args.CanProceed)
            {
                return;
            }

            switch (operationType)
            {
                case OperationType.Next:
                    if (selectedTabIndex == this.wizardTabControl1.TabCount - 1)
                    {
                        if (this.ParentForm != null)
                        {
                            this.ParentForm.DialogResult = DialogResult.OK;
                            this.ParentForm.Close();
                        }

                        return;
                    }

                    this.wizardTabControl1.SelectedIndex = selectedTabIndex + 1;
                    if (this.wizardTabControl1.TabCount == selectedTabIndex + 2)
                    {
                        this.nextButton.Text = @"&Finish";
                    }

                    break;

                case OperationType.Back:
                    this.wizardTabControl1.SelectedIndex = selectedTabIndex - 1;
                    this.nextButton.Text = @"&Next";
                    break;

                case OperationType.Cancel:
                    if (this.ParentForm != null)
                    {
                        this.ParentForm.DialogResult = DialogResult.Cancel;
                        this.ParentForm.Close();
                    }

                    break;
            }

            this.titleLabel.Text = this.wizardTabControl1.SelectedTab.Text;
            this.backButton.Enabled = this.wizardTabControl1.SelectedIndex > 0 && args.AllowBack;

            // nextButton.Enabled = wizardTabControl1.SelectedIndex < wizardTabControl1.TabCount - 1 && args.AllowNext;
            this.cancelButton.Enabled = args.AllowCancel;
        }

        #endregion Methods
    }
}
